package com.tungkong.utils;

//
// Created by 后端码匠 on 2022/11/30.
//

import android.content.Context;
import android.os.Environment;
import android.util.Log;

import java.io.File;

/**
 * UVCCamera.java中的静态函数加载so，已经完成对JNILogUtils.java的注册。
 */
public class JNILogUtils {

    private static Context globalAplicationContext = null;

    private static String PATH_LOGCAT = null;

    public enum LogLevel {
        LOG_LEVEL_NONE,
        LOG_LEVEL_ERR,
        LOG_LEVEL_WARNING,
        LOG_LEVEL_INFO,
        LOG_LEVEL_DEBUG
    }

    public static void init() {

        if (globalAplicationContext == null) return;

        boolean obtainSDcardAccess = false;
        try {
            obtainSDcardAccess = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
        } catch (Exception e) {
        }
        if (obtainSDcardAccess) {// 优先保存到SD卡中
            final File externalFilesDir = globalAplicationContext.getExternalFilesDir(null);
            if (externalFilesDir != null) {
                PATH_LOGCAT = externalFilesDir.getAbsolutePath() + File.separator + "kmsdk";
            } else {// 如果SD卡不存在，就保存到本应用的目录下
                PATH_LOGCAT = globalAplicationContext.getFilesDir().getAbsolutePath()
                        + File.separator + "kmsdk";
            }
        } else {// 如果SD卡不存在，就保存到本应用的目录下
            PATH_LOGCAT = globalAplicationContext.getFilesDir().getAbsolutePath()
                    + File.separator + "kmsdk";
        }

        File file = new File(PATH_LOGCAT);
        if (!file.exists()) {
            file.mkdirs();
        }
        logInit(PATH_LOGCAT, "km_media_log.txt", 4, 4);
        Log.e("LogUtils", "cur file dir is:" + file.toString());
    }

    public synchronized static void setApplicationContext(Context aplicationContext) {
        globalAplicationContext = aplicationContext.getApplicationContext();
    }

    // 日志类初始化
    public static native void logInit(String logFilePath, String logName, int logfileLevel, int logScreenLevel);

    public static native void logJni(int logLevel, String content);

    public static native void logClose();

}
